package approve.action;

import resource.pojo.Approvedoc;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import approval.dao.ApproveDocumentDAO;

public class My_viewform16 extends Activity {
	Bundle language;
	String lang = "EN", database;
	ApproveDocumentDAO addao = new ApproveDocumentDAO();
	TextView title, des, time, lbtitle, lbcweek, lbnweek, lbdes, lbtime;
	WebView cweek, nweek;
	Approvedoc AppDoc;
	String AppDocid;

	public void getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			language = getIntent().getExtras().getBundle(getLang());
			AppDocid = (String) getIntent().getExtras().get("AppDocID");
		}
	}

	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_viewform16);
		getDatabase();

		if (getIntent().getExtras().getString("delete") != null) {
			LinearLayout linearDelete = (LinearLayout) findViewById(R.id.linearDelete);
			Button btnDelete = new Button(this);
			btnDelete.setText(language.getString("Delete"));
			btnDelete.setOnClickListener(selectDelete);
			linearDelete.addView(btnDelete);
			linearDelete.setVisibility(View.VISIBLE);
		}

		lbtitle = (TextView) findViewById(R.id.l_la_title);
		lbcweek = (TextView) findViewById(R.id.l_la_currentWeek);
		lbnweek = (TextView) findViewById(R.id.l_la_nextWeek);
		lbdes = (TextView) findViewById(R.id.l_la_description);
		lbtime = (TextView) findViewById(R.id.l_la_time);

		title = (TextView) findViewById(R.id.l_weekly_title);
		cweek = (WebView) findViewById(R.id.l_lwv_currentWeek);
		nweek = (WebView) findViewById(R.id.l_lwv_nextWeek);
		des = (TextView) findViewById(R.id.l_weekly_description);
		time = (TextView) findViewById(R.id.l_dailyreport_time);

		lbtitle.setText(language.getString("title"));
		lbcweek.setText(language.getString("resultOfThisWeek"));
		lbnweek.setText(language.getString("planOfNextWeek"));
		lbdes.setText(language.getString("Description"));
		lbtime.setText(language.getString("st_ThoiGian"));

		AppDoc = addao.getOnce16(AppDocid, database);

		final String mimeType = "text/html";
		String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

		title.setText(AppDoc.getTitle());
		cweek.loadData(header + AppDoc.getContent(), mimeType, null);
		nweek.loadData(header + AppDoc.getContent2(), mimeType, null);
		des.setText(AppDoc.getDescription());
		time.setText(AppDoc.getTimecreate().toString());
	}

	OnClickListener selectDelete = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			dialogDelete();
		}
	};

	private void dialogDelete() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(language.getString("Delete"));
		alert.setMessage(language.getString("are-you-sure"));
		alert.setPositiveButton(language.getString("ok"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						ApproveDocumentDAO.moveApproveToTrash(AppDocid,
								database);
						goToDeclineDocument();
					}
				});
		alert.setNegativeButton(language.getString("cancel"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});

		AlertDialog dialog = alert.create();
		dialog.show();
	}

	private void goToDeclineDocument() {
		finish();
		Intent intent = new Intent(My_viewform16.this,
				My_App_DeclineDocument.class);
		Bundle extras = getIntent().getExtras();
		try{
			extras.remove("delete");
			extras.remove("AppDocID");
		}catch (Exception e) {
		}
		intent.putExtras(extras);
		startActivity(intent);
	}
}
